package com.examplemartin.jpademo.controller.stock;


import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.pojo.po.good.GoodPO;
import com.examplemartin.jpademo.pojo.po.stock.RepoInfoPO;
import com.examplemartin.jpademo.pojo.po.stock.StockPO;
import com.examplemartin.jpademo.pojo.po.stock.SummGoodPO;
import com.examplemartin.jpademo.pojo.vo.repoinfo.StockInfoVO;
import com.examplemartin.jpademo.pojo.vo.repoinfo.SummRepoVO;
import com.examplemartin.jpademo.service.GoodService;
import com.examplemartin.jpademo.service.RepoInfoService;
import com.examplemartin.jpademo.service.StockService;
import com.examplemartin.jpademo.service.stock.SummGoodService;
import com.examplemartin.jpademo.util.EntityCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/stock")
@Controller
@Slf4j
public class StockController {

    @Resource
    private StockService stockService;

    @Resource
    private GoodService goodService;

    @Resource
    private RepoInfoService repoInfoService;
    @Resource
    private SummGoodService summGoodService;

    /**
     * 跳转到 仓储页面
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/stocklist.do")
    public String stocklistDo(Model model) {
        model.addAttribute("title", "仓库设置");
        List<StockPO> all = stockService.findAll();
        model.addAttribute("stocklist", all);


        return "stock/stocklist";
    }

    /**
     * 跳转到添加仓库
     * addstock.do
     */
    @GetMapping("/addstock.do")
    public String addstockDo(Model model) {
        model.addAttribute("title", "添加仓库");

        List<GoodPO> all = goodService.findAll();
        model.addAttribute("goodlist", all);

        return "stock/addstock";
    }

    /**
     * ajax动作  添加仓库实体
     * addstock.ajax
     */
    @PostMapping("/addstock.ajax")
    @ResponseBody
    public AjaxResult addstockAjax(@RequestBody StockPO stockPO) {

        AjaxResult check = EntityCheckUtil.check(stockPO);
        if (check.getCode() != 200) {
            return AjaxResult.fail(500, check.getMsg());
        }

        List<StockPO> allByName = stockService.findAllByName(stockPO.getName());
        if (allByName.size() > 0) {
            return AjaxResult.fail(500, "创建仓库失败-库名重复");
        }

        AjaxResult save = stockService.save(stockPO);
        if (save.getCode() != 200) {
            return AjaxResult.fail(500, save.getMsg());
        }
        return AjaxResult.success(200, save.getMsg(), save.getData());
    }

    /**
     * 跳转到编辑仓库页面 在这里可定义库存阈值
     *
     * @param id
     * @param model
     *
     * @return
     */
    @GetMapping("/editstock.do")
    public String editstockDo(Long id, Model model) {
        model.addAttribute("title", "更新仓库");

        StockPO byId = stockService.findById(id);
        model.addAttribute("stock", byId);

        List<GoodPO> goodall = goodService.findAll();
        model.addAttribute("goodlist", goodall);

        // 查看指定仓库的信息
        List<RepoInfoPO> repoInfoAll = repoInfoService.findAllByStockPO(byId);


        ArrayList<RepoInfoPO> modelrepoInfoAll = new ArrayList<>();
        Boolean flag = false;
        for (GoodPO goodPO : goodall) {
            flag = false;
            for (RepoInfoPO repoInfoPO : repoInfoAll) {
                if (goodPO.getId().longValue() == repoInfoPO.getGoodPO().getId().longValue()) {
                    modelrepoInfoAll.add(repoInfoPO);
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                RepoInfoPO repoInfoPO = new RepoInfoPO();
                repoInfoPO.setId(null);
                repoInfoPO.setGoodPO(goodPO);
                repoInfoPO.setStockPO(byId);
                repoInfoPO.setThreshold(0);
                RepoInfoPO save = repoInfoService.save(repoInfoPO);
                modelrepoInfoAll.add(save);
            }

        }

        model.addAttribute("repoinfolist", modelrepoInfoAll);


        return "stock/editstock";
    }

    /**
     * ajax 保存仓库修改信息 /savestock.ajax
     *
     * @param stockPO
     *
     * @return
     */
    @PostMapping("/savestock.ajax")
    @ResponseBody
    public AjaxResult saveStock(@RequestBody StockPO stockPO) {
        AjaxResult save = stockService.saveModify(stockPO);

        if (save.getCode() != 200) {
            return AjaxResult.fail(500, save.getMsg());
        }
        return AjaxResult.success(200, save.getMsg(), save.getData());

    }

    /**
     * 修改仓储阈值
     *
     * @param list
     *
     * @return
     */
    @PostMapping("/savethresho.ajax")
    @ResponseBody
    public AjaxResult savethreshoAjax(@RequestBody List<RepoInfoPO> list) {
        AjaxResult ajaxResult = stockService.savethreshoAjax(list);
        if (ajaxResult.getCode() != 200) {
            return AjaxResult.fail(ajaxResult.getCode(), ajaxResult.getMsg());
        } else {
            return AjaxResult.success(ajaxResult.getCode(), ajaxResult.getMsg(), null);
        }
    }


    /**
     * /stock/repoinfolist.do
     * 库存信息
     */
    @GetMapping("repoinfolist.do")
    public String repoinfolistDo(@RequestParam(name = "stockid", required = false) Long stockid,//所在仓库
                                 @RequestParam(name = "likegoodname", required = false) String likegoodname, // 商品名
                                 @RequestParam(name = "repostatus", required = false) Integer repostatus, // 是否触碰阈值
                                 Model model) {
        model.addAttribute("title", "库存信息");
        List<RepoInfoPO> repoInfoPOS = stockService.dynamicfindAll(likegoodname, stockid, repostatus);
        model.addAttribute("repoInfoPOS", repoInfoPOS);
        model.addAttribute("stock", stockService.findAll());

        List<SummGoodPO> allsumm = summGoodService.findAll();
        ArrayList<SummRepoVO> summRepoVOS = new ArrayList<>();

        for (SummGoodPO summGoodPO : allsumm) {
            SummRepoVO summRepoVO = new SummRepoVO();
            summRepoVO.setGoodPO(summGoodPO.getGoodPO());
            summRepoVO.setGoodcount(summGoodPO.getGoodcount());
            summRepoVO.setGoodspare(summGoodPO.getGoodspare());
            //添加仓库存储信息
            summRepoVO.setStockinfolist(new ArrayList<StockInfoVO>());
            List<RepoInfoPO> repoInfoPOList = repoInfoService.findAllByGoodPO(summGoodPO.getGoodPO());
            for (RepoInfoPO repoInfoPO : repoInfoPOList) {
                StockPO stockPO = repoInfoPO.getStockPO();
                GoodPO goodPO = repoInfoPO.getGoodPO();
                Integer threshold = repoInfoPO.getThreshold();
                Integer goodcount = repoInfoPO.getGoodcount();
                StockInfoVO stockInfoVO = new StockInfoVO();
                stockInfoVO.setStockPO(stockPO);
                stockInfoVO.setGoodcount(goodcount);
                stockInfoVO.setGoodPO(goodPO);
                stockInfoVO.setGoodcount(goodcount);
                stockInfoVO.setThreshold(threshold);
                summRepoVO.getStockinfolist().add(stockInfoVO);
            }
            summRepoVOS.add(summRepoVO);
        }
        model.addAttribute("summlist", summRepoVOS);

        return "stock/stockrepolist";
    }

}
